package com.am.service.serviceImpl;

import com.am.bean.Role;
import com.am.bean.UserRole;
import com.am.dao.RoleMapper;
import com.am.dao.UserRoleMapper;
import com.am.service.RoleCheckService;
import com.utils.Meg;
import com.utils.RoleCheckUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleCheckServiceImpl implements RoleCheckService {
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private RoleMapper roleMapper;

    public Meg getRoleCheck(Integer userId){
        Meg meg  = Meg.success();

        List<Role> roleList = roleMapper.selectAll();
        List<UserRole> list= userRoleMapper.selectByUserId(userId);
        String[] ss = new String [list.size()];
        for (int i = 0 ; i<list.size();i++) {
            ss[i] = list.get(i).getRoleId().toString();
        }
        meg.add("data", RoleCheckUtil.getRoleCheck(roleList));
        meg.add("check",ss);
        return meg;
    }

    @Override
    public Meg roleCheck(Integer userId, Integer[] roleId, Integer type) {
        if(type==0){
            for (int i = 0; i < roleId.length; i++) {
                UserRole userRole = new UserRole();
                userRole.setRoleId(roleId[i]);
                userRole.setUserId(userId);
                userRoleMapper.insert(userRole);
            }
        }else{
            for (int i = 0; i < roleId.length; i++) {
                userRoleMapper.deleteByPrimaryKey(userId,roleId[i]);
            }
        }
        return Meg.success();
    }
}
